package com.aiti.lulian.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.constant.MessageCodeConstant;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.page.PageResult;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.gxmh.dto.GxmhPolicyDeclareDto;
import com.aiti.lulian.gxmh.dto.GxmhPolicyParamDto;
import com.aiti.lulian.gxmh.dto.GxmhPolicyVisitParamDto;
import com.aiti.lulian.gxmh.dto.GxmhPolicyVisitVo;
import com.aiti.lulian.gxmh.entity.GxmhPolicyVisit;
import com.aiti.lulian.gxmh.service.GovArticleService;
import com.aiti.lulian.gxmh.service.GxmhPolicyDeclareService;
import com.aiti.lulian.gxmh.vo.GxmhPolicyVo;
import com.aiti.lulian.service.GxmhPolicyVisitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * description:
 * auth fcb
 * date 2025-06-09 15:19
 **/
@RestController
@RequestMapping("/gxmh/policy")
@Api(tags = "工信门户政策")
@Slf4j
public class GxmhPolicyController extends WebController {

    @Autowired
    private GovArticleService govArticleService;
    @Autowired
    private GxmhPolicyDeclareService gxmhPolicyDeclareService;
    @Autowired
    private GxmhPolicyVisitService gxmhPolicyVisitService;

    @GetMapping("/generateFile")
    @ApiOperation(value = "爬取政策文件")
    public Message generateFile(@RequestParam(value = "type") String type, @RequestParam(value = "pageNum") String pageNum) {
        govArticleService.requestListPage(type, pageNum);
        return Success();
    }

    @PostMapping("/saveArticle")
    @ApiOperation(value = "保存爬取到的政策文件")
    public Message saveArticle(@RequestBody Map<String, String> map) {
        return govArticleService.saveArticle(map.get("uuid"), map.get("url")) ? Success() : Fail();
    }

    @PostMapping("/queryPolicyList")
    @ApiOperation(value = "查询政策列表")
    public Message queryPolicyList(@RequestBody GxmhPolicyParamDto queryDto) {
        try {
            PageResult<GxmhPolicyVo> gxmhPolicyVoPageResult = govArticleService.queryPolicyList(queryDto);
            return Success(gxmhPolicyVoPageResult);
        }catch (Exception e) {
            log.error("查询政策列表失败：{}", ExceptionUtil.stacktraceToString(e));
            return Fail("接口异常");
        }
    }

    @GetMapping("/queryPolicyCount")
    @ApiOperation(value = "查询政策数量")
    public Message queryPolicyCount() {
        return Success(govArticleService.queryPolicyCountList());
    }

    @GetMapping("/getArticle")
    @ApiOperation(value = "查询政策详情")
    public Message getArticle(@RequestParam(value = "baseId") String uuid) {
        try {
            GxmhPolicyVo article = govArticleService.getArticle(uuid);
            return Success(article);
        }catch (Exception e) {
            log.error("查询政策详情失败：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Fail("接口异常");
        }
    }

    @GetMapping("/getPolicyForm")
    @ApiOperation("查询政策对应的表单")
    public Message getPolicyForm(@RequestParam String policyId) {
        try {
            return Success(gxmhPolicyDeclareService.getFromDetail(policyId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return new Message(MessageCodeConstant.MSG_FAIL_CODE, e.getMessage(), null);
            }
            log.error("查询政策详情失败：{}", ExceptionUtil.stacktraceToString(e));
            return new Message(MessageCodeConstant.MSG_FAIL_CODE, "接口异常："+ e.getMessage(), null);
        }
    }

    @PostMapping("/savePolicyDeclare")
    @ApiOperation(value = "保存申报政策")
    public Message savePolicyDeclare(@RequestBody GxmhPolicyDeclareDto dto) {
        try {
            gxmhPolicyDeclareService.savePolicyDeclare(dto);
            return Success();
        }catch (Exception e) {
            log.error("保存申报政策失败：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @GetMapping("/getHotArticle")
    @ApiOperation(value = "查询最热门政策")
    public Message getHotArticle(@RequestParam(value = "province", required = false) String province,
                                 @RequestParam(value = "city", required = false) String city,
                                 @RequestParam(value = "county", required = false) String county) {
        try {
            GxmhPolicyVo article = govArticleService.getHotArticle(province, city, county);
            return Success(article);
        }catch (Exception e) {
            log.error("查询最热门政策失败：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @PostMapping("/insertPolicyVisit")
    @ApiOperation(value = "保存政策访问记录")
    public Message insertPolicyVisit(@RequestBody GxmhPolicyVisit gxmhPolicyVisit) {
        try {
            gxmhPolicyVisitService.insertPolicyVisit(gxmhPolicyVisit);
            return Success();
        }catch (Exception e) {
            log.error("保存政策访问记录失败：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Fail("接口异常：" + e.getMessage());
        }
    }

    @PostMapping("/queryPolicyVisitList")
    @ApiOperation(value = "查询政策访问记录")
    public Message queryPolicyVisitList(@RequestBody GxmhPolicyVisitParamDto queryDto) {
        try {
            PageResult<GxmhPolicyVisitVo> gxmhPolicyVoPageResult = gxmhPolicyVisitService.queryPolicyVisitList(queryDto);
            return Success(gxmhPolicyVoPageResult);
        }catch (Exception e) {
            log.error("查询政策访问记录失败：{}", ExceptionUtil.stacktraceToString(e));
            if(e instanceof BusinessException) {
                return Fail(e.getMessage());
            }
            return Fail("接口异常：" + e.getMessage());
        }
    }

}
